//
// Created by 11793 on 2025/11/7.
//
#include "stdio.h"
#include "db_init.h"

void init_database(sqlite3 *db){
    char *err_msg = 0;
    int rc;
    char *sql_users = "CREATE TABLE IF NOT EXISTS users ("
                      "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                      "username TEXT UNIQUE NOT NULL,"
                      "password TEXT NOT NULL,"
                      "role INTEGER NOT NULL);"; // 1:管理员, 0:操作员

    // 创建图书表
    char *sql_books = "CREATE TABLE IF NOT EXISTS books ("
                      "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                      "title TEXT NOT NULL,"
                      "author TEXT NOT NULL,"
                      "publisher TEXT,"
                      "isbn TEXT UNIQUE NOT NULL,"
                      "price REAL NOT NULL,"
                      "stock INTEGER NOT NULL DEFAULT 0,"
                      "category TEXT);";

    // 创建客户表
    char *sql_customers = "CREATE TABLE IF NOT EXISTS customers ("
                          "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                          "name TEXT NOT NULL,"
                          "phone TEXT UNIQUE,"
                          "email TEXT UNIQUE);";

    // 创建销售表
    char *sql_sales = "CREATE TABLE IF NOT EXISTS sales ("
                      "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                      "customer_id INTEGER,"
                      "sale_date TEXT NOT NULL,"
                      "total_amount REAL NOT NULL,"
                      "FOREIGN KEY(customer_id) REFERENCES customers(id));";

    // 创建销售明细表
    char *sql_sale_items = "CREATE TABLE IF NOT EXISTS sale_items ("
                           "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                           "sale_id INTEGER,"
                           "book_id INTEGER,"
                           "quantity INTEGER NOT NULL,"
                           "unit_price REAL NOT NULL,"
                           "FOREIGN KEY(sale_id) REFERENCES sales(id),"
                           "FOREIGN KEY(book_id) REFERENCES books(id));";

    // 执行建表语句
    rc = sqlite3_exec(db, sql_users, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "创建用户表错误: %s\n", err_msg);
        sqlite3_free(err_msg);
    }

    rc = sqlite3_exec(db, sql_books, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "创建图书表错误: %s\n", err_msg);
        sqlite3_free(err_msg);
    }

    rc = sqlite3_exec(db, sql_customers, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "创建客户表错误: %s\n", err_msg);
        sqlite3_free(err_msg);
    }

    rc = sqlite3_exec(db, sql_sales, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "创建销售表错误: %s\n", err_msg);
        sqlite3_free(err_msg);
    }

    rc = sqlite3_exec(db, sql_sale_items, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "创建销售明细表错误: %s\n", err_msg);
        sqlite3_free(err_msg);
    }

    // 初始化默认用户（如果用户表为空）
    char *check_sql = "SELECT COUNT(*) FROM users;";
    sqlite3_stmt *stmt;
    rc = sqlite3_prepare_v2(db, check_sql, -1, &stmt, 0);
    if (rc == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW) {
        int count = sqlite3_column_int(stmt, 0);
        if (count == 0) {
            // 添加默认管理员（用户名: admin, 密码: admin123）
            char *admin_sql = "INSERT INTO users (username, password, role) VALUES ('admin', 'admin123', 1);";
            sqlite3_exec(db, admin_sql, 0, 0, &err_msg);

            // 添加默认操作员（用户名: operator, 密码: op123）
            char *op_sql = "INSERT INTO users (username, password, role) VALUES ('operator', 'op123', 0);";
            sqlite3_exec(db, op_sql, 0, 0, &err_msg);
            printf("已创建默认用户：\n");
            printf("管理员：admin / admin123\n");
            printf("操作员：operator / op123\n");
        }
    }
    sqlite3_finalize(stmt);
}

